import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;

public class LeetCode1657 {
    public static void main(String[] args) {

    }

    /**
     * 思路
     * 1.判断两个字符串长度是否相等，不等则直接返回false
     * 2.判断两个字符串是否出现了相同的字母，不等则直接返回false，使用hashset来判断
     * 3.判断两个字符串是否有一样的字母次数，例如A字符串：a出现1次，b出现两次，c出现3次，B字符串：a出现3次，b出现1次，c出现两次
     * @param word1
     * @param word2
     * @return
     */
    public boolean closeStrings(String word1, String word2) {

        if (word1.length() != word2.length()){
            return false;
        }

        char[] w1 = word1.toCharArray();
        char[] w2 = word2.toCharArray();

        int length = w1.length;
        HashMap<Character, Integer> map1 = new HashMap<Character, Integer>(length);
        HashMap<Character, Integer> map2 = new HashMap<Character, Integer>(length);

        for(int i = 0; i < length; i++){
            map1.put(w1[i], map1.getOrDefault(w1[i], 0) + 1);
            map2.put(w2[i], map2.getOrDefault(w2[i], 0) + 1);
        }

        if(!map1.keySet().equals(map2.keySet())){
            return false;
        }

        ArrayList<Integer> s1 = new ArrayList<>(map1.values());
        ArrayList<Integer> s2 = new ArrayList<>(map2.values());

        Comparator<? super Integer> comparator = (Comparator<Integer>) (o1, o2) -> {
            if(o1.equals(o2)){
               return 0;
            }

            if(o1 > o2){
                return -1;
            }

            return 1;
        };
        s1.sort(comparator);
        s2.sort(comparator);
        return s1.equals(s2);
    }
}
